package com.dataexa.ky.jefflow.mxgraph;

import com.dataexa.ky.jefflow.flowcell.JefFlowParser;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * 解析MxGrapth的图形信息
 */
public class MxGraphParser implements JefFlowParser {

    /**
     * XML 文件
     */
    private final String xml;

    /**
     * 图形信息
     * @param xml
     */
    public MxGraphParser(String xml) {
        this.xml = xml;
    }

    /**
     * 解析MxGraph
     * @return 解析
     * @throws ParserConfigurationException 解析配置异常
     * @throws SAXException SAX 异常
     * @throws IOException Io Exception
     */
    public MxGraphModel parseMxGraph() throws Exception {

        return new MxGraphModel(parse());
    }

    @Override
    public Document parse() throws Exception {
        //1.创建Reader对象
        SAXReader reader = new SAXReader();
        reader.setEncoding("utf-8");
        //2.加载xml
        Document document = reader.read(new ByteArrayInputStream(xml.getBytes()));
        return document;
    }
}
